
library(readr)
library(DescTools)
study3 <- read_csv("study3.csv")

##education
study3$education[study3$education==1]<-0
study3$education[study3$education==2]<-0
study3$education[study3$education==3]<-1
study3$education[study3$education==4]<-1
study3$education[study3$education==5]<-2
study3$education[study3$education==6]<-2
study3$education[study3$education==7]<-2
study3$education[study3$education==8]<-2
study3$education[study3$education==9]<-2
study3$education[study3$education==10]<-NA
study3$education[study3$education==11]<-NA

study3$Male<-study3$gender
study3$Male[study3$gender==1]<-1
study3$Male[study3$gender==2]<-0
study3$Male[study3$gender==3]<-0

study3$latino[study3$latino==2]<-0
table(study3$latino)

study3$white<-study3$race_1
study3$white[is.na(study3$race_1)==TRUE]<-0
table(study3$white)


study3$black<-study3$race_2
study3$black[is.na(study3$race_2)==TRUE]<-0
table(study3$black)

table(study3$income)

study3$income2<-study3$income
study3$income2[study3$income==13]<-0
study3$noincome<-study3$income2
study3$noincome[study3$noincome>0]<-1
study3$noincome<-1-study3$noincome

study3$married<-study3$marital
study3$married[study3$marital>1]<-0
table(study3$married)

study3$kids<-2-study3$kids
table(study3$kids)

study3$daughter<-study3$daughters
study3$daughter[is.na(study3$daughters)==TRUE]<-0
study3$daughter[study3$daughters==1]<-0
study3$daughter[study3$daughters==2]<-1
study3$daughter[study3$daughters==3]<-1
table(study3$daughter)

study3$son<-study3$daughters
study3$son[is.na(study3$daughters)==TRUE]<-0
study3$son[study3$daughters==1]<-1
study3$son[study3$daughters==2]<-0
study3$son[study3$daughters==3]<-1
table(study3$son)
table(study3$daughters)


study3$relimp<-5-study3$relimp

study3$bagain<-study3$born.again
study3$bagain[is.na(study3$born.again)==TRUE]<-0
study3$bagain[study3$born.again==2]<-0
table(study3$bagain)

study3$chatt<-5-study3$chatt
study3$chatt[study3$chatt2==2]<-5
table(study3$chatt)


study3$pid<-study3$party1
study3$pid[study3$Dem==1]<-0
study3$pid[study3$Dem==2]<-1
study3$pid[study3$rep==2]<-5
study3$pid[study3$rep==1]<-6
study3$pid[study3$ind==1]<-4
study3$pid[study3$ind==2]<-2
#study3$pid[study3$pid_ind==3]<-2
table(study3$Dem)
table(study3$pid)

study3$AS1_1[study3$AS1_1==5]<-2
study3$AS1_1[study3$AS1_1==6]<-3
study3$AS1_1[study3$AS1_1==8]<-4
study3$AS1_1[study3$AS1_1==9]<-5
study3$AS1_1[study3$AS1_1==10]<-6

study3$AS1_2[study3$AS1_2==5]<-2
study3$AS1_2[study3$AS1_2==6]<-3
study3$AS1_2[study3$AS1_2==8]<-4
study3$AS1_2[study3$AS1_2==9]<-5
study3$AS1_2[study3$AS1_2==10]<-6

study3$AS1_3[study3$AS1_3==5]<-2
study3$AS1_3[study3$AS1_3==6]<-3
study3$AS1_3[study3$AS1_3==8]<-4
study3$AS1_3[study3$AS1_3==9]<-5
study3$AS1_3[study3$AS1_3==10]<-6

study3$AS1_4[study3$AS1_4==5]<-2
study3$AS1_4[study3$AS1_4==6]<-3
study3$AS1_4[study3$AS1_4==8]<-4
study3$AS1_4[study3$AS1_4==9]<-5
study3$AS1_4[study3$AS1_4==10]<-6

study3$AS1_5[study3$AS1_5==5]<-2
study3$AS1_5[study3$AS1_5==6]<-3
study3$AS1_5[study3$AS1_5==8]<-4
study3$AS1_5[study3$AS1_5==9]<-5
study3$AS1_5[study3$AS1_5==10]<-6

study3$AS1_6[study3$AS1_6==5]<-2
study3$AS1_6[study3$AS1_6==6]<-3
study3$AS1_6[study3$AS1_6==8]<-4
study3$AS1_6[study3$AS1_6==9]<-5
study3$AS1_6[study3$AS1_6==10]<-6

study3$AS2_1[study3$AS2_1==5]<-2
study3$AS2_1[study3$AS2_1==6]<-3
study3$AS2_1[study3$AS2_1==8]<-4
study3$AS2_1[study3$AS2_1==9]<-5
study3$AS2_1[study3$AS2_1==10]<-6

study3$AS2_2[study3$AS2_2==5]<-2
study3$AS2_2[study3$AS2_2==6]<-3
study3$AS2_2[study3$AS2_2==8]<-4
study3$AS2_2[study3$AS2_2==9]<-5
study3$AS2_2[study3$AS2_2==10]<-6

study3$AS2_3[study3$AS2_3==5]<-2
study3$AS2_3[study3$AS2_3==6]<-3
study3$AS2_3[study3$AS2_3==8]<-4
study3$AS2_3[study3$AS2_3==9]<-5
study3$AS2_3[study3$AS2_3==10]<-6

study3$AS2_4[study3$AS2_4==5]<-2
study3$AS2_4[study3$AS2_4==6]<-3
study3$AS2_4[study3$AS2_4==8]<-4
study3$AS2_4[study3$AS2_4==9]<-5
study3$AS2_4[study3$AS2_4==10]<-6

study3$AS2_5[study3$AS2_5==5]<-2
study3$AS2_5[study3$AS2_5==6]<-3
study3$AS2_5[study3$AS2_5==8]<-4
study3$AS2_5[study3$AS2_5==9]<-5
study3$AS2_5[study3$AS2_5==10]<-6

study3$AS3_1[study3$AS3_1==5]<-2
study3$AS3_1[study3$AS3_1==6]<-3
study3$AS3_1[study3$AS3_1==8]<-4
study3$AS3_1[study3$AS3_1==9]<-5
study3$AS3_1[study3$AS3_1==10]<-6

study3$AS3_2[study3$AS3_2==5]<-2
study3$AS3_2[study3$AS3_2==6]<-3
study3$AS3_2[study3$AS3_2==8]<-4
study3$AS3_2[study3$AS3_2==9]<-5
study3$AS3_2[study3$AS3_2==10]<-6

study3$AS3_3[study3$AS3_3==5]<-2
study3$AS3_3[study3$AS3_3==6]<-3
study3$AS3_3[study3$AS3_3==8]<-4
study3$AS3_3[study3$AS3_3==9]<-5
study3$AS3_3[study3$AS3_3==10]<-6

study3$AS3_4[study3$AS3_4==5]<-2
study3$AS3_4[study3$AS3_4==6]<-3
study3$AS3_4[study3$AS3_4==8]<-4
study3$AS3_4[study3$AS3_4==9]<-5
study3$AS3_4[study3$AS3_4==10]<-6

study3$AS3_5[study3$AS3_5==5]<-2
study3$AS3_5[study3$AS3_5==6]<-3
study3$AS3_5[study3$AS3_5==8]<-4
study3$AS3_5[study3$AS3_5==9]<-5
study3$AS3_5[study3$AS3_5==10]<-6

study3$AS3_6[study3$AS3_6==5]<-2
study3$AS3_6[study3$AS3_6==6]<-3
study3$AS3_6[study3$AS3_6==8]<-4
study3$AS3_6[study3$AS3_6==9]<-5
study3$AS3_6[study3$AS3_6==10]<-6

study3$AS4_1[study3$AS4_1==5]<-2
study3$AS4_1[study3$AS4_1==6]<-3
study3$AS4_1[study3$AS4_1==8]<-4
study3$AS4_1[study3$AS4_1==9]<-5
study3$AS4_1[study3$AS4_1==10]<-6

study3$AS4_2[study3$AS4_2==5]<-2
study3$AS4_2[study3$AS4_2==6]<-3
study3$AS4_2[study3$AS4_2==8]<-4
study3$AS4_2[study3$AS4_2==9]<-5
study3$AS4_2[study3$AS4_2==10]<-6

study3$AS4_3[study3$AS4_3==5]<-2
study3$AS4_3[study3$AS4_3==6]<-3
study3$AS4_3[study3$AS4_3==8]<-4
study3$AS4_3[study3$AS4_3==9]<-5
study3$AS4_3[study3$AS4_3==10]<-6

study3$AS4_4[study3$AS4_4==5]<-2
study3$AS4_4[study3$AS4_4==6]<-3
study3$AS4_4[study3$AS4_4==8]<-4
study3$AS4_4[study3$AS4_4==9]<-5
study3$AS4_4[study3$AS4_4==10]<-6

study3$AS4_5[study3$AS4_5==5]<-2
study3$AS4_5[study3$AS4_5==6]<-3
study3$AS4_5[study3$AS4_5==8]<-4
study3$AS4_5[study3$AS4_5==9]<-5
study3$AS4_5[study3$AS4_5==10]<-6


## build sexism measures
study3$hostile_sexism<-study3$AS1_3+study3$AS1_6+study3$AS2_1+
  study3$AS2_2+study3$AS2_3

study3$ben_sexism<-study3$AS1_1+study3$AS1_2+study3$AS1_4+
  study3$AS1_5+study3$AS2_4+study3$AS2_5

study3$ben_men<-study3$AS3_1+study3$AS3_3+study3$AS3_4+
  study3$AS4_1+study3$AS4_3

study3$hostil_men<-study3$AS3_2+study3$AS3_5+study3$AS3_6+
  study3$AS4_2+study3$AS4_4+study3$AS4_5


#Code all Missing as 0
study3$Control1[is.na(study3$Control1)]<-0
study3$Control2[is.na(study3$Control2)]<-0
study3$Control3[is.na(study3$Control3)]<-0
study3$Control4[is.na(study3$Control4)]<-0
study3$Control5[is.na(study3$Control5)]<-0
study3$Control6[is.na(study3$Control6)]<-0
study3$Control7[is.na(study3$Control7)]<-0
study3$Control8[is.na(study3$Control8)]<-0

study3$After1[is.na(study3$After1)]<-0
study3$After2[is.na(study3$After2)]<-0
study3$After3[is.na(study3$After3)]<-0
study3$After4[is.na(study3$After4)]<-0
study3$After5[is.na(study3$After5)]<-0
study3$After6[is.na(study3$After6)]<-0
study3$After7[is.na(study3$After7)]<-0
study3$After8[is.na(study3$After8)]<-0

study3$Before1[is.na(study3$Before1)]<-0
study3$Before2[is.na(study3$Before2)]<-0
study3$Before3[is.na(study3$Before3)]<-0
study3$Before4[is.na(study3$Before4)]<-0
study3$Before5[is.na(study3$Before5)]<-0
study3$Before6[is.na(study3$Before6)]<-0
study3$Before7[is.na(study3$Before7)]<-0
study3$Before8[is.na(study3$Before8)]<-0

study3$Girl1[is.na(study3$Girl1)]<-0
study3$Girl2[is.na(study3$Girl2)]<-0
study3$Girl3[is.na(study3$Girl3)]<-0
study3$Girl4[is.na(study3$Girl4)]<-0
study3$Girl5[is.na(study3$Girl5)]<-0
study3$Girl6[is.na(study3$Girl6)]<-0
study3$Girl7[is.na(study3$Girl7)]<-0
study3$Girl8[is.na(study3$Girl8)]<-0


study3$Boy1[is.na(study3$Boy1)]<-0
study3$Boy2[is.na(study3$Boy2)]<-0
study3$Boy3[is.na(study3$Boy3)]<-0
study3$Boy4[is.na(study3$Boy4)]<-0
study3$Boy5[is.na(study3$Boy5)]<-0
study3$Boy6[is.na(study3$Boy6)]<-0
study3$Boy7[is.na(study3$Boy7)]<-0
study3$Boy8[is.na(study3$Boy8)]<-0


study3$Respect<-study3$Control1+study3$Before1+
  study3$After1+study3$Boy1+study3$Girl1-1

study3$Obedient<-2-(study3$Control2+study3$Before2+
                           study3$After2+study3$Boy2+study3$Girl2)

study3$GoodMannered<-study3$Control3+study3$Before3+
  study3$After3+study3$Boy3+study3$Girl3-1

study3$WellBehaved<-study3$Control4+study3$Before4+
  study3$After4+study3$Boy4+study3$Girl4-1

study3$Polite<-2-(study3$Control5+study3$Before5+
                         study3$After5+study3$Boy5+study3$Girl5)

study3$Orderly<-2-(study3$Control6+study3$Before6+
                          study3$After6+study3$Boy6+study3$Girl6)

study3$Disciplined<-2-(study3$Control7+study3$Before7+
                              study3$After7+study3$Boy7+study3$Girl7)

study3$Loyal<-2-(study3$Control8+study3$Before8+
                        study3$After8+study3$Boy8+study3$Girl8)
table(study3$Loyal)

study3$Respect[study3$Respect==-1]<-NA
study3$Obedient[study3$Obedient==2]<-NA
study3$GoodMannered[study3$GoodMannered==-1]<-NA
study3$WellBehaved[study3$WellBehaved==-1]<-NA
study3$Polite[study3$Polite==2]<-NA
study3$Orderly[study3$Orderly==2]<-NA
study3$Disciplined[study3$Disciplined==2]<-NA
study3$Loyal[study3$Loyal==2]<-NA


#table(study3$Therm_1)
study3$Therm_Trump<-100-study3$Therm_1
study3$Therm_Biden<-100-study3$Therm_2
study3$Therm_Dem<-100-study3$Therm_3
study3$Therm_GOP<-100-study3$Therm_4
study3$Therm_BLM<-100-study3$Therm_5
study3$Therm_Cops<-100-study3$Therm_6

study3$Border<-4-study3$border
study3$Wall<-4-study3$wall
study3$Deport<-4-study3$deport
study3$FamilySep<-4-study3$familysep

study3$Immigration<-(study3$Border+study3$Wall+study3$Deport+
                            study3$FamilySep)/4

study3$Marriage<-study3$marriage-1
study3$Conversion<-study3$conversion-1
study3$GR<-.5*(study3$Marriage+study3$Conversion)


study3$DeathPen<-4-study3$deathpen
study3$Lethal<-4-study3$lethalinj
study3$DP<-.5*(study3$DeathPen+study3$Lethal)

study3$control<-study3$FL_13_DO_Control
study3$control[is.na(study3$control)]<-0

study3$boy<-study3$FL_13_DO_Boy
study3$boy[is.na(study3$boy)]<-0

study3$girl<-study3$FL_13_DO_Girl
study3$girl[is.na(study3$girl)]<-0

study3$before<-study3$FL_13_DO_Before
study3$before[is.na(study3$before)]<-0

study3$after<-study3$FL_13_DO_After
study3$after[is.na(study3$after)]<-0

study3$treatment<-study3$control+2*study3$boy+
  3*study3$girl+4*study3$after+5*study3$before

study3$treatment[study3$treatment==0]<-NA

table(study3$treatment)

study3$south<-NA
study3$south[study3$State==1]<-1 #AL
study3$south[study3$State==4]<-1 #AR
study3$south[study3$State==9]<-1#DC
study3$south[study3$State==10]<-1#FL
study3$south[study3$State==11]<-1#GA
study3$south[study3$State==18]<-1#KY
study3$south[study3$State==19]<-1#LA
study3$south[study3$State==21]<-1#MD
study3$south[study3$State==25]<-1#MS
study3$south[study3$State==34]<-1#NC
study3$south[study3$State==37]<-1#OK
study3$south[study3$State==42]<-1#sc
study3$south[study3$State==44]<-1#TN
study3$south[study3$State==45]<-1#TX
study3$south[study3$State==48]<-1#VA
study3$south[study3$State==50]<-1#WV
study3$south[is.na(study3$south)]<-0


summary(study3$bagain)

study3$Catholic<-NA
study3$Protest<-NA
study3$Evangel<-NA
study3$Agnositc<-NA

study3$Catholic[study3$denom==2]<-1
study3$Catholic[is.na(study3$Catholic)]<-0

study3$Protest[study3$denom==1]<-1
study3$Protest[is.na(study3$Protest)]<-0
study3$Protest[study3$bagain==0]<-0

study3$Evangel[study3$denom==1]<-1
study3$Evangel[is.na(study3$Protest)]<-0
study3$Evangel[study3$bagain==1]<-1

study3$Agnostic[study3$denom==9]<-1
study3$Agnostic[study3$denom==10]<-1
study3$Agnostic[study3$denom==12]<-1

study3$Agnostic[is.na(study3$Agnostic)]<-0


summary(study3$denom)



study3$Catholic[study3$denom==2]<-1
study3$Catholic[is.na(study3$Catholic)]<-0

study3$south[study3$State==50]<-1#WV










study3$auth <- (study3$Respect +
                       study3$Obedient+
                       study3$GoodMannered+
                       study3$WellBehaved+
                       study3$Polite+
                       study3$Orderly+
                       study3$Disciplined+
                       study3$Loyal)



#Freq(study3$treatment)

table(study3$treatment)

